import requests
from bs4 import BeautifulSoup


headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
    'Host': 'movie.douban.com'
}


movie_list = []
director_list = []
time_list = []
star_list = []

for i in range(0, 10):
    link = 'https://movie.douban.com/top250?start=' + str(i*25)
    res = requests.get(link,headers = headers,timeout = 10)

    soup = BeautifulSoup(res.text,"lxml")
    div_list = soup.find_all('div', class_ = 'hd')
    div1_list = soup.find_all('div', class_='bd')
    div2_list = soup.find_all('div', class_='star')

    for each in div_list:
        movie = each.a.span.text.strip()
        movie_list.append(movie)

    for each in div1_list:
        info = each.p.text.strip()
        if len(info) < 3:
            continue
        time_start = info.find('20')
        if time_start < 0:
            time_start = info.find('19')
        end = info.find('...')
        time = info[end + 32:end + 36]
        time_list.append(time)

        end = info.find('主')
        director = info[4:end - 3]
        director_list.append(director)

    for each in div2_list:
        info = each.text.strip()
        star = info[0:3]
        star_list.append(star)


for i in range(0, 250):
    print("%s, %s,%s" % (movie_list[i], director_list[i], star_list[i]))
